% 演示各个维度下的驻波
% 例如波动方程和“盒中粒子”无势能薛定谔方程的本征解的空间部分是驻波
% Gitee Repo

clc
clear

% 维度 dim=1,2,3
dim = 3;
% 各个方向上的波峰+波谷个数
n=[5;5;5];
% 是否取平方
squared=1;

L=5;
dx=0.1;

figure()
hold on
axis equal

if dim <= 0 || dim > 3
    error('wrong dim');
    return;
end

switch dim
    case 1
        x = (0:dx:L)';
        Psi = sin(n(1)*pi/L*x);
        if squared == 1
            Psi=Psi.^2;
        end
        plot(x,Psi)
    case 2
        [x y] = meshgrid(0:dx:L);
        Psi = sin(n(1)*pi/L*x).*sin(n(2)*pi/L*y);
        if squared == 1
            Psi=Psi.^2;
        end
        h = surf(x,y,Psi)
        set(h,'edgecolor','none');
    case 3
        [x y z] = meshgrid(0:dx:L);
        Psi = sin(n(1)*pi/L*x).*sin(n(2)*pi/L*y).*sin(n(3)*pi/L*z);
        if squared == 1
            Psi=Psi.^2;
        end
        h=slice(x,y,z,Psi,[],[],1);
        set(h,'edgecolor','none');
        % 导出数据，便于使用plotly查看
        save('standingwaves.mat','x','y','z','Psi','-v7');
end

